﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
namespace WPFExcelReport
{
    public partial class TDRAUFTR
    {
        #region ==ExTemplate===========
        public static readonly DependencyProperty ExTemplateProperty = DependencyProperty.Register("ExTemplate",
            typeof(string), typeof(TDRAUFTR),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnExTemplateChanged));
        public string ExTemplate
        {
            get
            {
                return (string)GetValue(ExTemplateProperty);
            }
            set
            {
                SetValue(ExTemplateProperty, value);
                if (ITXEXRPT != null)
                {
                    ITXEXRPT.strExcelFile = value;
                }
            }
        }
        private static void OnExTemplateChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TDRAUFTR;
            if (src != null)
            {
                src.ExTemplate = (string)e.NewValue;
            }
        }
        #endregion
        #region ==DocType===========
        public static readonly DependencyProperty DocTypeProperty = DependencyProperty.Register("DocType",
            typeof(string), typeof(TDRAUFTR),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnDocTypeChanged));
        public string DocType
        {
            get
            {
                return (string)GetValue(DocTypeProperty);
            }
            set
            {
                SetValue(DocTypeProperty, value);
                TMV.DocType = value;
                if (ITXEXRPT != null)
                {
                    ITXEXRPT.DocType = value;
                }
            }
        }
        private static void OnDocTypeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TDRAUFTR;
            if (src != null)
            {
                src.DocType = (string)e.NewValue;
            }
        }
        #endregion

        #region ==DESTFOLDER===========
        public static readonly DependencyProperty DESTFOLDERProperty = DependencyProperty.Register("DESTFOLDER",
            typeof(string), typeof(TDRAUFTR),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnDESTFOLDERChanged));
        public string DESTFOLDER
        {
            get
            {
                return (string)GetValue(DESTFOLDERProperty);
            }
            set
            {
                SetValue(DESTFOLDERProperty, value);
                TMV.DESTFOLDER = value;
                if (ITXEXRPT != null)
                {
                    ITXEXRPT.destfolder = value;
                }
            }
        }
        private static void OnDESTFOLDERChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TDRAUFTR;
            if (src != null)
            {
                src.DESTFOLDER = (string)e.NewValue;
            }
        }
        #endregion

        #region ==PREFIX===========
        public static readonly DependencyProperty PREFIXProperty = DependencyProperty.Register("PREFIX",
            typeof(string), typeof(TDRAUFTR),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPREFIXChanged));
        public string PREFIX
        {
            get
            {
                return (string)GetValue(PREFIXProperty);
            }
            set
            {
                SetValue(PREFIXProperty, value);
                TMV.PREFIX = value;
                if (ITXEXRPT != null)
                {
                    ITXEXRPT.prefix = value;
                }
            }
        }
        private static void OnPREFIXChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TDRAUFTR;
            if (src != null)
            {
                src.PREFIX = (string)e.NewValue;
            }
        }
        #endregion

        #region ==COPYS===========
        public static readonly DependencyProperty COPYSProperty = DependencyProperty.Register("COPYS",
            typeof(int), typeof(TDRAUFTR),
            new FrameworkPropertyMetadata(1, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCOPYSChanged));
        public int COPYS
        {
            get
            {
                return (int)GetValue(COPYSProperty);
            }
            set
            {
                SetValue(COPYSProperty, value);
                TMV.COPYS = value;
                if (ITXEXRPT != null)
                {
                    ITXEXRPT.COPYS = value;
                }
            }
        }
        private static void OnCOPYSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TDRAUFTR;
            if (src != null)
            {
                src.COPYS = (int)e.NewValue;
            }
        }
        #endregion
        
        #region ==PrintType===========
        public static readonly DependencyProperty PrintTypeProperty = DependencyProperty.Register("PrintType",
            typeof(string), typeof(TDRAUFTR),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPrintTypeChanged));
        public string PrintType
        {
            get
            {
                return (string)GetValue(PrintTypeProperty);
            }
            set
            {
                SetValue(PrintTypeProperty, value);
                TMV.PrintType = value;
                if (ITXEXRPT != null)
                {
                    ITXEXRPT.PrintType = value;
                }
            }
        }
        private static void OnPrintTypeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TDRAUFTR;
            if (src != null)
            {
                src.PrintType = (string)e.NewValue;
            }
        }
        #endregion
        #region ==IsInfo===========
        public static readonly DependencyProperty IsInfoProperty = DependencyProperty.Register("IsInfo",
            typeof(bool), typeof(TDRAUFTR),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsInfoChanged));
        public bool IsInfo
        {
            get
            {
                return (bool)GetValue(IsInfoProperty);
            }
            set
            {
                SetValue(IsInfoProperty, value);
            }
        }
        private static void OnIsInfoChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TDRAUFTR;
            if (src != null)
            {
                src.IsInfo = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==Root===========
        public static readonly DependencyProperty RootProperty = DependencyProperty.Register("Root",
            typeof(string), typeof(TDRAUFTR),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnRootChanged));
        public string Root
        {
            get
            {
                return (string)GetValue(RootProperty);
            }
            set
            {
                SetValue(RootProperty, value); 
                if (ITXEXRPT != null)
                {
                    ITXEXRPT.Root = value;
                }
            }
        }
        private static void OnRootChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TDRAUFTR;
            if (src != null)
            {
                src.Root = (string)e.NewValue;
            }
        }
        #endregion

         
        public DataSet SetExchange
        {
            get
            {

                return ITXEXRPT.SetExchange;
            }
        }
       
        public VDRAUFTR TMV
        {
            get
            {
                return (VDRAUFTR)MV;
            }
        }


        public ITXEXRPT ITXEXRPT
        {
            get
            {
                return TMV.ITXEXRPT;
            } 
        }
        public VHELP VHP
        {
            get
            {
                if (!ENVMAIN.ContainsKey("VHP"))
                {
                    ENVMAIN.Add("VHP", new VHELP());
                }
                return (VHELP)ENVMAIN["VHP"];
            }
        }
        public TDRAUFTR()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VDRAUFTR();
            this.DataContext = MV;
            InitializeComponent();
        }
           
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            if (IsInfo)
                EventDispatcher.Instance.OnMessage += DR03_OnMessage; 
            base.Window_Loaded(sender, e);

        } 
        void DR03_OnMessage(object sender, MessageEventArgs e)
        {
            if (e.Info.Where != "ITXNATIVE")
                return;
            PM["DOCTYPE"] = e.Info.What;
            PM["DEFAULTUI"] = e.Info.P1;
            PM["UI"] = e.Info.Details;
            PM["EVENTS"] = e;  
            if(e.Info.Details.Contains("VKD01IT "))
            {
                PM["KRS"] = "16";
                PM["LFD"] = "000023";
                SResult = VHP.VKK.VKKLOAD(PM);
                DocType = "ABCOMPARE";
                PrintType = "OpenExcel";
                ITXEXRPT.strExcelFile = @"ITX\ITXABDOC.xls";
                PRINT(SResult);
            }
            //NormalTask = Task.Factory.StartNew(() => StartDocument(PM), new CancellationTokenSource().Token, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());

            //获取界面，获取工作流事件参数，创建界面，传递这个工作流ID
            //如果界面已经存在，则仅仅赋值
        }
        public void PRINT(DataSet Set)
        {
            try
            {
                ITXEXRPT.dsSource = Set; 
                string filename = ITXEXRPT.WriteExcelReport(PrintType); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void PRINT(DataSet Set,bool IsQuite)
        {
            try
            {
                TMV.PRINT(Set, IsQuite);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void PrintCommand(object sender, RoutedEventArgs e)
        {
            base.PrintCommand(sender, e);
        }
        private void PrintViewCommand(object sender, RoutedEventArgs e)
        {

        }
    }
}
